//
//  MHTopicHeadrView.m
//  ManHua
//
//  Created by lanou on 15/10/28.
//  Copyright © 2015年 蓝欧科技. All rights reserved.
//

#import "MHTopicHeadrView.h"

#import <UIImageView+WebCache.h>
@interface MHTopicHeadrView()<UIScrollViewDelegate>
@property(nonatomic,strong)UIScrollView *mainScrollerView;
@property(nonatomic,strong)UIImageView *carouselImg;
@property(nonatomic,strong)UIPageControl *pageControl;
@property (nonatomic, strong) NSTimer *timer;

@end
@implementation MHTopicHeadrView

/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */
-(instancetype)initWithFrame:(CGRect)frame
{
    self=[super initWithFrame:frame];
    if (self) {
        self.mainScrollerView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, Width, 640.0 / 1024.0 * Width)];
        self.mainScrollerView.delegate=self;
        self.mainScrollerView.pagingEnabled=YES;
        self.mainScrollerView.contentSize=CGSizeMake(4*Width, 0);
        for (int i=0; i<4; i++) {
            _carouselImg=[[UIImageView alloc]initWithFrame:CGRectMake(i*Width, 0, Width, CGRectGetMaxY(self.mainScrollerView.frame))];
#pragma mark -给轮播图的图片添加点击手势
            _carouselImg.userInteractionEnabled=YES;
            UITapGestureRecognizer *tapRecognizer=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(skipToMorePageBook:)];
            _carouselImg.tag=1000+i;
            [_carouselImg addGestureRecognizer:tapRecognizer];
            
            [self.mainScrollerView addSubview:_carouselImg];
            
        }
        [self addSubview:_mainScrollerView];
        self.pageControl=[[UIPageControl alloc]initWithFrame:CGRectMake(Width-80, CGRectGetMaxY(self.mainScrollerView.frame)-40, 60, 30)];
        self.pageControl.numberOfPages=3;
        [self.pageControl addTarget:self action:@selector(changeScrollerViewcontentOffset) forControlEvents:UIControlEventValueChanged];
        
        [self addSubview:_pageControl];
        
    }
    return self;
}
-(void)setCarouseDatasourceArr:(NSMutableArray *)carouseDatasourceArr
{
    _carouseDatasourceArr=carouseDatasourceArr;
    for (int i=0; i<self.mainScrollerView.subviews.count; i++) {
        UIImageView *imageView=(UIImageView *)self.mainScrollerView.subviews[i];
        
        if (i<self.mainScrollerView.subviews.count-1) {
            MHTopicBannerModel *bannerModel=(MHTopicBannerModel *)carouseDatasourceArr[i];
            
            [imageView sd_setImageWithURL:[NSURL URLWithString:bannerModel.TopicIconUrl]];
            
        }
        else
        {
            [imageView sd_setImageWithURL:[NSURL URLWithString:((MHTopicBannerModel *)carouseDatasourceArr[0]).TopicIconUrl]];
        }
        
    }
    [self addTimer];
    
    
}
-(void)skipToMorePageBook:(UITapGestureRecognizer *)tapRecognized
{
    
    NSInteger tagNumber=tapRecognized.view.tag-1000;
    if (tagNumber==0||tagNumber==3) {
        MHTopicBannerModel *carouselBannerModel=self.carouseDatasourceArr[0];
        if (self.tapToBookCVCBlock!=nil) {
            self.tapToBookCVCBlock(carouselBannerModel);
        }
    }
    else
    {MHTopicBannerModel *carouselBannerModel=self.carouseDatasourceArr[tagNumber];
        if (self.tapToBookCVCBlock!=nil) {
            
            
            self.tapToBookCVCBlock(carouselBannerModel);
        }
        
    }
}
-(void)nextImge
{
    if (_pageControl.currentPage<2) {
        _pageControl.currentPage++;
        [self changeScrollerViewcontentOffset];
    }
    else if(_pageControl.currentPage == 2)
    {
        _pageControl.currentPage = 0;
        [self changeScrollerViewcontentOffset];
    }
    
}
-(void)changeScrollerViewcontentOffset
{
    
    NSInteger pageNumber=self.pageControl.currentPage;
    if (pageNumber==0) {
        [self.mainScrollerView setContentOffset:CGPointMake(3*Width, 0) animated:YES];
    }
    else
    {
        [self.mainScrollerView setContentOffset:CGPointMake(pageNumber *Width, 0) animated:YES];
    }
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView.contentOffset.x<0) {
        [scrollView setContentOffset:CGPointMake(Width*3, 0) animated:NO];
        
    }
    else if (scrollView.contentOffset.x+Width>scrollView.contentSize.width)
    {
        [scrollView setContentOffset:CGPointMake(0, 0) animated:NO];
    }
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    CGFloat pageNumbe =scrollView.contentOffset.x/Width;
    if (pageNumbe==3) {
        self.pageControl.currentPage=0;
    }
    else
    {
        self.pageControl.currentPage=(NSInteger)pageNumbe;
    }
    [self addTimer];
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self removeTimer];
}

- (void)addTimer
{
    self.timer = [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(nextImge) userInfo:nil repeats:YES];
}
- (void)removeTimer
{
    [self.timer invalidate];
}

@end
